﻿using Role_el.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace Role_el.Controllers
{
    public class CheckController : ApiController
    {
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="UserName"></param>
        /// <param name="CheckTime1"></param>
        /// <param name="CheckTime2"></param>
        /// <param name="CheckState"></param>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult GetChList(int page, int limit,string UserName, string CheckTime1, string CheckTime2, string CheckState)
        {
            using (RolesDBEntities db = new RolesDBEntities())
            {
                var talist = db.UserChecks.AsEnumerable();
                if (!string.IsNullOrEmpty(UserName))
                {
                    talist = talist.Where(p => p.UserName.Contains(UserName));
                }
                if (!string.IsNullOrEmpty(CheckTime1))
                {
                    talist = talist.Where(p => p.CheckTime>=Convert.ToDateTime(CheckTime1));
                }
                if (!string.IsNullOrEmpty(CheckTime2))
                {
                    talist = talist.Where(p => p.CheckTime <= Convert.ToDateTime(CheckTime2));
                }
                if (!string.IsNullOrEmpty(CheckState))
                {
                    talist = talist.Where(p => p.CheckState==CheckState);
                }
                var count = talist.Count();
                var list = talist.Select(p => new
                {
                    UCId = p.UCId,
                    UserName = p.UserName,
                    CheckTime= p.CheckTime,
                    CheckState=p.CheckState,
                    CheckOutTime= p.CheckOutTime,
                    IsCanCel=p.IsCanCel
                });
                return Json(new { count = count, data = list.OrderBy(p => p.UCId).Skip((page - 1) * limit).Take(limit).ToList() });
            }
        }
        /// <summary>
        /// 签退
        /// </summary>
        /// <param name="uc"></param>
        /// <returns></returns>
        public IHttpActionResult resetuser([FromBody] UserChecks uc)
        {
            using (RolesDBEntities db = new RolesDBEntities())
            {
                var year = DateTime.Now.Year;
                var month = DateTime.Now.Month;
                var day = DateTime.Now.Day;
                var start = new DateTime(year, month, day, 0, 0, 0);
                var end = new DateTime(year, month, day, 23, 59, 59);
                var checkuser = db.UserChecks.FirstOrDefault(x => x.UserId == uc.UserId && x.CheckTime >= start && x.CheckTime <= end);
                TimeSpan ts3 = DateTime.Now.Subtract(Convert.ToDateTime(checkuser.CheckTime)).Duration();
                int a = (int)Math.Round(ts3.TotalMinutes);
                //如果查到今天签到的用户则可以签退 必须在签到后的15分钟后才可以签退
                if (checkuser != null)
                {
                    if (checkuser.CheckState != "已签到")
                    {
                        return Json(new { code = -2, msg = "该用户未签到！" });
                    }
                    else
                    {
                        if (checkuser.IsCanCel != 0)
                        {
                            return Json(new { code = -3, msg = "该用户已签退！" });
                        }
                        else
                        {
                            if (a >= 10)
                            {
                                checkuser.IsCanCel =1;
                                checkuser.CheckOutTime = DateTime.Now;
                                int n = db.SaveChanges();
                                if (n > 0)
                                {
                                    return Json(new { code = 0, msg = "签退成功！" });
                                }
                                else
                                {
                                    return Json(new { code = -4, msg = "签退失败！" });
                                }
                            }
                            else
                            {
                                return Json(new { code = -5, msg = "时间未到，不能签退！" });
                            }
                        }
                    }
                }
                else
                {
                    return Json(new { code = -1, msg = "错误操作！" });
                }
            }
        }
        /// <summary>
        /// 签到
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="time"></param>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult up(int UserId, string time)
        {
            using (RolesDBEntities db = new RolesDBEntities())
            {
                var year = DateTime.Now.Year;
                var month = DateTime.Now.Month;
                var day = DateTime.Now.Day;
                var start = new DateTime(year, month, day, 0, 0, 0);
                var end = new DateTime(year, month, day, 23, 59, 59);
                var checkuser = db.UserChecks.FirstOrDefault(x => x.UserId == UserId && x.CheckTime >= start && x.CheckTime <= end);
                if (checkuser != null)
                {
                    if (checkuser.CheckState == "已签到")
                    {
                        return Json(new { code = -2, msg = "您今天已经签到" });
                    }
                    else
                    {
                        checkuser.CheckState = "已签到";
                        int n = db.SaveChanges();
                        if (n > 0)
                        {
                            return Json(new { code = 0, msg = "签到成功" });
                        }
                        else
                        {
                            return Json(new { code = -1, msg = "签到失败" });

                        }
                    }

                }
                else
                {
                    var username = db.Users.FirstOrDefault(x => x.Id == UserId);
                    UserChecks theuser = new UserChecks();
                    theuser.UserId = UserId;
                    theuser.CheckTime = DateTime.Now;
                    theuser.CheckState = "已签到";
                    theuser.UserName = username.LoginName;
                    theuser.IsCanCel = 0;
                    db.UserChecks.Add(theuser);
                    int n = db.SaveChanges();
                    if (n > 0)
                    {
                        return Json(new { code = 1, msg = "签到成功" });
                    }
                    else
                    {
                        return Json(new { code = -3, msg = "签到失败" });

                    }
                }
            }
        }
        
    }
}
